package com.hs.hlw.credit.system.business.service.impl;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.FieldService;
import com.hs.hlw.credit.system.data.secondary.mapper.ColumnValMapper;
import com.hs.hlw.credit.system.data.secondary.model.ColumnVal;
import com.hs.hlw.credit.system.data.secondary.model.ColumnValExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * FieldService
 * @author zhb
 * @date 2018/11/19
 */
@Service
public class FieldServiceImpl implements FieldService {
    private @Autowired ColumnValMapper columnValMapper;

    /**
     * 获取指定数据库表下的字段
     * @param tableId
     * @return
     */
    public ResponseDto getFieldListByTable(String tableId) {
        ColumnValExample example = new ColumnValExample();
        ColumnValExample.Criteria criteria = example.createCriteria();
        criteria.andXmlidEqualTo(tableId);
        List<ColumnVal> list = columnValMapper.selectByExample(example);

        List<Map<String, Object>> _list = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (ColumnVal columnVal : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("fieldName", columnVal.getColcnname());
                map.put("fieldCode", columnVal.getColname());
                map.put("fieldDataType", columnVal.getDatatype());
                map.put("fieldLength", columnVal.getLength());
                _list.add(map);
            }
        }

        return new ResponseDto("获取成功", _list);
    }
}